package com.jiang.action;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.jiang.utils.FileUploadUtils;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileUploadAction extends ActionSupport {

	private File photo;
	private String photoFileName; // 固定格式文件原名称
	private String photoContentType; // 固定格式文件原类型

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String upload() {
		System.out.println("临时文件的名称:" + this.photo.getName() + ", 文件的大小:" + this.photo.length());
		System.out.println("原始文件的名称:" + this.photoFileName + ",文件类型名称:" + this.photoContentType);
		// 重新创建不重复的文件名称
		boolean flag = false;
		if(photo!=null){
			String fileName = FileUploadUtils.createFileName(this.photoContentType);
			String filePath = ServletActionContext.getRequest().getServletContext().getRealPath("/") + fileName;
			// 直接调用上传方法
			flag = FileUploadUtils.uploadFile(photo, filePath);
		}
		// 不跳转页面
		if(!flag){
			return INPUT;
		}
		return NONE;
	}

}
